package com.qeegoo.b2bautozityp.moudle.good.viewmodel.carmodel;

import android.databinding.ObservableField;
import android.support.v4.app.Fragment;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.adapter.CarModelOneAdapter;
import com.qeegoo.b2bautozityp.moudle.good.entity.MySection;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelOneBean;

import java.util.ArrayList;

/**
 * 王少岩 在 2017/9/25 创建了它
 */

public class CarModelOneViewModel {
    public ObservableField<String> title = new ObservableField<>("");
    private RequestApi mRequestApi;
    private CarModelOneAdapter mAdapter;
    private ArrayList<MySection<CarModelOneBean.CarSeriesVo>> mMySections = new ArrayList<>();
    private Fragment mFragment;

    public CarModelOneViewModel(Fragment fragment, RequestApi requestApi) {
        mFragment = fragment;
        mRequestApi = requestApi;
        mAdapter = new CarModelOneAdapter(mMySections);
    }

    public CarModelOneAdapter getAdapter() {
        return mAdapter;
    }

    public void getListCarSeries(String carLogoId){
        mRequestApi.listCarSeries(HttpParams.getListCarSeries(carLogoId))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<CarModelOneBean>>() {
                    @Override
                    public void onNext(ArrayList<CarModelOneBean> carModelOneBeen) {
                        for (CarModelOneBean bean:carModelOneBeen){
                            mMySections.add(new MySection<>(true, bean.brandName));
                            for (CarModelOneBean.CarSeriesVo vo:bean.carSeriesVoList){
                                mMySections.add(new MySection<>(vo));
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });

    }

    public ReplyCommand leftCommand = new ReplyCommand(()-> mFragment.getFragmentManager().popBackStack());
}
